package com.tiger;

import com.tiger.auth.ApiRequest;
import com.tiger.auth.AuthToken;

import java.util.HashMap;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        //客户端侧
        long timestamp = System.currentTimeMillis();
        String appId = "123";
        String baseUrl = "http://localhost:9001/test";
        Map<String,String> params = new HashMap<>();
        params.put("appId",appId);
        AuthToken authToken = AuthToken.create(baseUrl,System.currentTimeMillis(),params);
        String fullUrl = baseUrl + "?token="+authToken.getToken()+"&timestamp="+timestamp+"&appId=" + appId;

        //服务端侧
        ApiRequest apiRequest = ApiRequest.parseFromFullUrl(fullUrl);
        String appId1 = apiRequest.getAppId();
        String baseUrl1 = apiRequest.getBaseUrl();
        long timestamp1 = apiRequest.getTimestamp();
        String token = apiRequest.getToken();

        AuthToken clientToken = new AuthToken(token,timestamp1);
        if(clientToken.isExpire()){
            throw new RuntimeException("token is expired!");
        }
        Map<String,String> serverParams = new HashMap<>();
        serverParams.put("appId",appId1);
        AuthToken serverToken = AuthToken.create(baseUrl1,timestamp1,serverParams);
        if(!serverToken.match(clientToken)){
            throw new RuntimeException("token vertiation fail!");
        }
    }
}
